package com.nearbymap.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nearbymap.maps.DirectionsService;
import com.nearbymap.maps.GeocodingService;
import com.nearbymap.maps.PlacesService;
import com.nearbymap.maps.StaticMapsService;
import com.nearbymap.model.Address;
import com.nearbymap.model.DirectionsResponse;
import com.nearbymap.model.LatLng;
import com.nearbymap.model.MeetSearch;
import com.nearbymap.model.Place;
import com.nearbymap.model.PlacesResponse;

@Controller
public final class MeetController {
	@Autowired
	private DirectionsService directionsService;
	@Autowired
	private StaticMapsService staticMapsService;
	@Autowired
	private GeocodingService geocodingService;
	@Autowired
	private PlacesService placesService;

	@RequestMapping(value = "/",  method = RequestMethod.GET)
	public String base(Model model) {
		return "redirect:/meet";
	}
	@RequestMapping(value = "/meet",  method = RequestMethod.GET)
	public String meet(Model model) {
		MeetSearch meetSearch = new MeetSearch();
		meetSearch.setMapUri(staticMapsService.getMapUri());
		model.addAttribute(meetSearch);
		return "meet";
	}
	@RequestMapping(value = "/meet",  method = RequestMethod.POST)
	public String doMeetSearch(@ModelAttribute MeetSearch meetSearch, Model model) {
		
		String origin1 = meetSearch.getStartPoint1();
		Address origin1Geocode = geocodingService.getGeocode(origin1, false);
		LatLng origin1LatLng = origin1Geocode.getLatLng();
		System.out.println("origin1LatLng: " + origin1LatLng);

		String origin2 = meetSearch.getStartPoint2();
		Address origin2Geocode = geocodingService.getGeocode(origin2, false);
		LatLng origin2LatLng = origin2Geocode.getLatLng();
		System.out.println("origin2LatLng: " + origin2LatLng);

		PlacesResponse placesResponse = this.placesService.getPlaces(origin1LatLng.calcMidPoint(origin2LatLng), ((int) origin1LatLng.calcDistance(origin2LatLng)) + 1000);
		List<Place> places = placesResponse.getPlaces();
		System.out.println("places: " + places);

		DirectionsResponse directions = directionsService.getDirectionsXml(origin1, origin2, places.get(0).getName());
		String polyline = directions.getOverviewPolyline();
		System.out.println("OverviewPolyline: " + polyline);

		meetSearch.setMapUri(staticMapsService.getMapUri(polyline, origin1LatLng, origin2LatLng, places.get(0).getLocation()));
		System.out.println(meetSearch);
		model.addAttribute(meetSearch);
		return "meet";
	}
	public DirectionsService getDirectionsService() {
		return directionsService;
	}
	public void setDirectionsService(DirectionsService directionsService) {
		this.directionsService = directionsService;
	}
}